﻿#include "MemsAutoTestSoftware.h"

#include "SetTurnTableAndThermostatDialog.h"
#include "SetMEMSDataDialog.h"
#include "SetScriptDialog.h"
#include "ParameterConsole.h"
#include "TurnTableRunningStateManager.h"
#include "MEMSDataHandler.h"
#include "ScriptRunner.h"
#include "ScriptSaveLoader.h"
#include "LogSaver.h"
#include "SerialPortManager.h"
#include "TurnTableCodec.h"

#include <qcolor.h>

MemsAutoTestSoftware::MemsAutoTestSoftware(QWidget* parent) :QMainWindow(parent)
{
  ui.setupUi(this);
  initAllUI();
  initSignalsAndSlots();
}
MemsAutoTestSoftware::~MemsAutoTestSoftware()
{
  disconnect();
}
void MemsAutoTestSoftware::slot_file_loadScript()//打开加载脚本对话框
{
  logSaver->saveLog(QStringLiteral("打开加载脚本对话框"), LogSaver::Information);
  QFileDialog dialog(this);
  dialog.setAcceptMode(QFileDialog::AcceptOpen);
  dialog.setNameFilter(".script(*.script)");
  QString filepath;
  if (QDialog::Accepted == dialog.exec())
    {
      filepath = dialog.selectedFiles().first();
      if (!filepath.isEmpty())
        {
          scriptSaveLoader->loadScriptFile(filepath);
        }
    }
  return;
}
void MemsAutoTestSoftware::slot_file_saveScript()//打开保存脚本对话框
{
  logSaver->saveLog(QStringLiteral("打开保存脚本对话框"), LogSaver::Information);
  QFileDialog dialog(this);
  dialog.setAcceptMode(QFileDialog::AcceptSave);
  dialog.setNameFilter(".script(*.script)");
  QString filepath;
  if (dialog.exec()) {
      filepath = dialog.selectedFiles().first();
    }
  if (!filepath.isEmpty()) {
      if (!(scriptSaveLoader->saveAsScriptFile(filepath)))
        {
          showMessage(QStringLiteral("保存脚本时格式转换出错"));
        }
    }
  return;
}
void MemsAutoTestSoftware::slot_file_about()//关于转台
{
  QMessageBox::information(this, QStringLiteral("关于"), QStringLiteral("本软件只保证每个操作本身的正确性，具体是否应该执行某个操作，请三思而后行。"));
}
void MemsAutoTestSoftware::slot_control_turnTable_connect()
{
  if (serialPortManager_turnTable->slot_openListener())
    {
      logSaver->saveLog(QStringLiteral("打开转台串口连接成功"), LogSaver::Information);
      showMessage(QStringLiteral("打开转台串口连接成功"));
      turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_Connect, 0, 0, 0);
      return;
    }
  logSaver->saveLog(QStringLiteral("打开转台串口连接失败"), LogSaver::Information);
  showMessage(QStringLiteral("打开转台串口连接失败"));
  return;
}
void MemsAutoTestSoftware::slot_control_turnTable_disconnect()
{
  serialPortManager_turnTable->slot_closeListener();
  logSaver->saveLog(QStringLiteral("断开转台串口连接成功"), LogSaver::Information);
  emit turnTableCodec->signal_turnTableConnectionState(TurnTableCodec::State_Disconnected);
}

void MemsAutoTestSoftware::slot_control_turnTable_run()
{
  turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_SetLoad, setTurnTableAndThermostatDialog->getLoadMode(), 0, 0);
  TurnTableCodec::TurnTableRunningMode runningMode = setTurnTableAndThermostatDialog->getRunningMode();
  switch (runningMode)
    {
    case TurnTableCodec::Mode_PositionMode:
      if (!(turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_SetPositionMode,
                                            setTurnTableAndThermostatDialog->getPosition(),
                                            setTurnTableAndThermostatDialog->getRate(),
                                            setTurnTableAndThermostatDialog->getAcceleration())))
        {
          return;
        }
      else
        {
          break;
        }
    case TurnTableCodec::Mode_RateMode:
      if (!(turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_SetRateMode,
                                            setTurnTableAndThermostatDialog->getRate(),
                                            setTurnTableAndThermostatDialog->getAcceleration(), 0)))
        {
          return;
        }
      else
        {
          break;
        }
    default:
      return;
    }
  turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_Run, runningMode, 0, 0);
}
void MemsAutoTestSoftware::slot_control_thermostat_run()
{


}

void MemsAutoTestSoftware::slot_control_dataReader_connect()
{
  if (serialPortManager_mEMSData->slot_openListener())
    {
      ui.action_control_dataReader_connect->setDisabled(true);
      ui.action_control_dataReader_disconnect->setDisabled(false);
      ui.label_mEMSData_stateFlag_connection->setStyleSheet("background:green");
      showMessage(QStringLiteral("MEMS数据串口已连接"));
    }
  else
    {
      QMessageBox::critical(this, QStringLiteral("错误"), QStringLiteral("打开MEMS数据串口连接失败"));
    }
}

void MemsAutoTestSoftware::slot_control_dataReader_disconnect()
{
  serialPortManager_mEMSData->slot_closeListener();
  ui.action_control_dataReader_connect->setDisabled(false);
  ui.action_control_dataReader_disconnect->setDisabled(true);
  ui.label_mEMSData_stateFlag_connection->setStyleSheet("background:red");
  showMessage(QStringLiteral("MEMS数据串口已断开"));
}

void MemsAutoTestSoftware::slot_updateTurnTableConnectionState(TurnTableCodec::ConnectionState connectionState)
{
  switch (connectionState)
    {
    case TurnTableCodec::State_Disconnected:
      serialPortManager_turnTable->slot_closeListener();
      //转台温箱连接状态指示灯
      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:red");
      ui.label_thermostat_stateFlag_connection->setStyleSheet("background:lightgray");

      //转台运行状态指示灯
      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:lightgray");
      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:lightgray");
      ui.label_turnTable_stateFlag_running->setStyleSheet("background:lightgray");
      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:lightgray");

      //对应每个与转台连接相关的按钮
      ui.action_control_turnTable_connect->setDisabled(false);
      ui.action_control_turnTable_disconnect->setDisabled(true);
      ui.action_control_turnTable_powerOn->setDisabled(true);
      ui.action_control_turnTable_powerOff->setDisabled(true);
      ui.action_control_turnTable_closeCircuit->setDisabled(true);
      ui.action_control_turnTable_openCircuit->setDisabled(true);
      ui.action_control_turnTable_run->setDisabled(true);
      ui.action_control_turnTable_stop->setDisabled(true);
      ui.action_control_turnTable_emergencyStop->setDisabled(true);
      ui.action_control_thermostat_connect->setDisabled(true);
      ui.action_control_thermostat_disconnect->setDisabled(true);
      ui.action_control_thermostat_run->setDisabled(true);

      //数值显示
      ui.label_turnTable_currentPosition->setText("---");
      ui.label_turnTable_currentRate->setText("---");
      ui.lineEdit_turnTable_runMode->setText("---");
      ui.lineEdit_turnTable_targetPosition->setText("---");
      ui.lineEdit_turnTable_targetRate->setText("---");
      ui.lineEdit_turnTable_targetAcceleration->setText("---");
      ui.lineEdit_currentTemperature->setText("---");
      ui.lineEdit_targetTemperature->setText("---");
      ui.lineEdit_temperatureRate->setText("---");
      showMessage(QStringLiteral("已从转台断开"));
      break;
    case TurnTableCodec::State_Connected:
      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:green");
      ui.label_thermostat_stateFlag_connection->setStyleSheet("background:red");
      ui.action_control_turnTable_connect->setDisabled(true);
      ui.action_control_turnTable_disconnect->setDisabled(false);
      ui.action_control_thermostat_connect->setDisabled(false);
      showMessage(QStringLiteral("已连接到转台"));
      break;
    case TurnTableCodec::State_Connecting:
      ui.label_turnTable_stateFlag_connection->setStyleSheet("background:yellow");
      ui.action_control_turnTable_connect->setDisabled(true);
      ui.action_control_turnTable_disconnect->setDisabled(true);
      showMessage(QStringLiteral("正在连接转台"));
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_updateTurnTableCurrentState(float position, float rate)//更新转台状态：运行位置和速率
{
  ui.label_turnTable_currentPosition->setText(QString::number(position, 'f', accuracy));
  ui.label_turnTable_currentRate->setText(QString::number(rate, 'f', accuracy));
  if (turnTableRunningStateManager->getState_turnTable_running()== TurnTableCodec::State_TurnTableRunning)
    {
      ui.frame_turnTableStateShower->paintNewState(position,rate, turnTableRunningStateManager->getState_turnTable_targetRate());
    }
}
void MemsAutoTestSoftware::slot_updateCurrentThermostatTemperature(float temperature)//更新温箱温度
{
  ui.lineEdit_currentTemperature->setText(QString::number(temperature, 'f', accuracy));
}
void MemsAutoTestSoftware::slot_updateTurnTablePowerSupplyState(TurnTableCodec::TurnTablePowerSupplyState powerSupplyState)
{
  switch (powerSupplyState)
    {
    case TurnTableCodec::State_PowerOn:
      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:green");
      ui.action_control_turnTable_powerOn->setDisabled(true);
      if (turnTableRunningStateManager->getState_turnTable_circuit() == TurnTableCodec::State_CircuitOpened)
        {
          ui.action_control_turnTable_powerOff->setDisabled(false);
        }
      else
        {
          ui.action_control_turnTable_powerOff->setDisabled(true);
        }
      break;
    case TurnTableCodec::State_PowerOff:
      ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:red");
      ui.action_control_turnTable_powerOn->setDisabled(false);
      ui.action_control_turnTable_powerOff->setDisabled(true);
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_updateTurnTableCricuitState(TurnTableCodec::TurnTableCircuitState circuitState)
{
  switch (circuitState)
    {
    case TurnTableCodec::State_CircuitClosed:
      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:green");
      ui.action_control_turnTable_closeCircuit->setDisabled(true);
      if (turnTableRunningStateManager->getState_turnTable_running() == TurnTableCodec::State_TurnTableStoped)
        {
          ui.action_control_turnTable_openCircuit->setDisabled(false);
        }
      else
        {
          ui.action_control_turnTable_openCircuit->setDisabled(true);
        }
      break;
    case TurnTableCodec::State_CircuitOpened:
      ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:red");
      ui.action_control_turnTable_openCircuit->setDisabled(true);
      if (turnTableRunningStateManager->getState_turnTable_powerSupply() == TurnTableCodec::State_PowerOn)
        {
          ui.action_control_turnTable_closeCircuit->setDisabled(false);
        }
      else
        {
          ui.action_control_turnTable_closeCircuit->setDisabled(true);
        }
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_updateTurnTableAmplifierState(TurnTableCodec::TurnTableAmplifierState amplifierState)
{
  switch (amplifierState)
    {
    case TurnTableCodec::State_AmplifierBroken:
      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:red");
      break;
    case TurnTableCodec::State_AmplifierNormal:
      ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:green");
      break;
    default:
      break;
    }

}
void MemsAutoTestSoftware::slot_updateTurnTableRunningState(TurnTableCodec::TurnTableRunningState runningState)
{
  switch (runningState)
    {
    case TurnTableCodec::State_TurnTableRunning:
      ui.label_turnTable_stateFlag_running->setStyleSheet("background:green");
      ui.action_control_turnTable_run->setDisabled(true);
      ui.action_control_turnTable_stop->setDisabled(false);
      ui.action_control_turnTable_emergencyStop->setDisabled(false);
      break;
    case TurnTableCodec::State_TurnTableStoped:
      ui.label_turnTable_stateFlag_running->setStyleSheet("background:red");
      ui.action_control_turnTable_stop->setDisabled(true);
      ui.action_control_turnTable_emergencyStop->setDisabled(true);
      if (turnTableRunningStateManager->getState_turnTable_circuit() == TurnTableCodec::State_CircuitClosed)
        {
          ui.action_control_turnTable_run->setDisabled(false);
        }
      else
        {
          ui.action_control_turnTable_run->setDisabled(true);
        }
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_turnTableErrorEcho(TurnTableCodec::ErrorFlag errorFlag)
{
  switch (errorFlag)
    {
    case TurnTableCodec::Error_ProcessState:
      showMessage(QStringLiteral("转台错误：过程状态错误"));
      break;
    case TurnTableCodec::Error_AxleState:
      showMessage(QStringLiteral("转台错误：轴状态错误"));
      break;
    case TurnTableCodec::Error_AxleNum:
      showMessage(QStringLiteral("转台错误：轴号错误"));
      break;
    case TurnTableCodec::Error_CheckSummation:
      showMessage(QStringLiteral("转台错误：检查和错误"));
      break;
    case TurnTableCodec::Error_TurnTableAddress:
      showMessage(QStringLiteral("转台错误：转台设备地址错误"));
      break;
    case TurnTableCodec::Error_Load:
      showMessage(QStringLiteral("转台错误：负载错误"));
      break;
    case TurnTableCodec::Error_PositionModeOverLimited:
      showMessage(QStringLiteral("转台错误：位置模式参数超限"));
      break;
    case TurnTableCodec::Error_RateModeOverLimited:
      showMessage(QStringLiteral("转台错误：速率模式参数超限"));
      break;
    case TurnTableCodec::Error_OtherUnfitCondition:
      showMessage(QStringLiteral("转台错误：其他不符合限制条件的错误"));
      break;
    case TurnTableCodec::Error_ThermostatTargetTemperatureFailed:
      showMessage(QStringLiteral("转台错误：温箱目标温度设置失败"));
      break;
    case TurnTableCodec::Error_ThermostatTargetTemperatureOverLimited:
      showMessage(QStringLiteral("转台错误：温箱目标温度超限"));
      break;
    case TurnTableCodec::Error_ThermostatFeedbackError:
      showMessage(QStringLiteral("转台错误：温箱应答帧错误"));
      break;
    case TurnTableCodec::Error_Connection:
      showMessage(QStringLiteral("转台错误：建立通讯错误"));
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_instructionEcho(TurnTableCodec::TurnTableInstructionFlag instructionFlag, float arg1, float arg2, float arg3)
{
  switch (instructionFlag)
    {
    case TurnTableCodec::TurnTable_SetLoad:
      break;
    case TurnTableCodec::TurnTable_SetPositionMode:
      slot_turnTablePositionModeEcho(arg1, arg2, arg3);
      break;
    case TurnTableCodec::TurnTable_SetRateMode:
      slot_turnTableRateModeEcho(arg1, arg2);
      break;
    case TurnTableCodec::TurnTable_Run:
      break;
    case TurnTableCodec::Thermostat_Connect:
      slot_thermostatConnected();
      break;
    case TurnTableCodec::Thermostat_Disconnect:
      slot_thermostatDisconnected();
      break;
    case TurnTableCodec::Thermostat_TargetTemperature:
      slot_thermostatTargetTemperatureEcho(arg1);
      break;
    case TurnTableCodec::Thermostat_TemperatureRate:
      slot_thermostatTemperatureRateEcho(arg1);
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_thermostatConnected()
{
  ui.label_thermostat_stateFlag_connection->setStyleSheet("background:green");
  ui.action_control_thermostat_connect->setDisabled(true);
  ui.action_control_thermostat_disconnect->setDisabled(false);
  ui.action_control_thermostat_run->setDisabled(false);
  showMessage(QStringLiteral("温箱已连接"));
}
void MemsAutoTestSoftware::slot_thermostatDisconnected()
{
  ui.label_thermostat_stateFlag_connection->setStyleSheet("background:red");
  ui.action_control_thermostat_connect->setDisabled(false);
  ui.action_control_thermostat_disconnect->setDisabled(true);
  ui.action_control_thermostat_run->setDisabled(true);
  ui.lineEdit_currentTemperature->setText("---");
  ui.lineEdit_targetTemperature->setText("---");
  ui.lineEdit_temperatureRate->setText("---");
  showMessage(QStringLiteral("温箱已断开"));
}
void MemsAutoTestSoftware::slot_turnTablePositionModeEcho(float position, float rate, float acceleration)//转台设置运行模式：位置
{
  ui.lineEdit_turnTable_runMode->setText(QStringLiteral("位置模式"));
  ui.lineEdit_turnTable_targetPosition->setText(QString::number(position, 'f', accuracy));
  ui.lineEdit_turnTable_targetRate->setText(QString::number(rate, 'f', accuracy));
  ui.lineEdit_turnTable_targetAcceleration->setText(QString::number(acceleration, 'f', accuracy));
}
void MemsAutoTestSoftware::slot_turnTableRateModeEcho(float rate, float acceleration)//转台设置运行模式：速率
{
  ui.lineEdit_turnTable_runMode->setText(QStringLiteral("速率模式"));
  ui.lineEdit_turnTable_targetPosition->setText("---");
  ui.lineEdit_turnTable_targetRate->setText(QString::number(rate, 'f', accuracy));
  ui.lineEdit_turnTable_targetAcceleration->setText(QString::number(acceleration, 'f', accuracy));
}
void MemsAutoTestSoftware::slot_thermostatTargetTemperatureEcho(float temperature)
{
  ui.lineEdit_targetTemperature->setText(QString::number(temperature));
}
void MemsAutoTestSoftware::slot_thermostatTemperatureRateEcho(float temperatureRate)
{
  ui.lineEdit_temperatureRate->setText(QString::number(temperatureRate));
}

//脚本管理器反馈
void MemsAutoTestSoftware::slot_scriptIsStarted()
{
  ui.action_file_saveScript->setDisabled(true);
  ui.action_file_loadScript->setDisabled(true);
  ui.action_control_script_startScript->setDisabled(true);
  ui.action_control_script_stopScript->setDisabled(false);
  ui.menu_control_turnTable->setDisabled(true);
  ui.menu_control_thermostat->setDisabled(true);
  ui.menu_control_dataReader->setDisabled(true);
  ui.action_set_turnTableAndThermostat->setDisabled(true);
  ui.action_set_dataReader->setDisabled(true);
  ui.action_set_script->setDisabled(true);
  ui.action_set_parameter->setDisabled(true);
  showMessage(QStringLiteral("脚本已开始运行"));
}
void MemsAutoTestSoftware::slot_scriptIsStoped()
{
  ui.action_file_saveScript->setDisabled(false);
  ui.action_file_loadScript->setDisabled(false);
  ui.action_control_script_startScript->setDisabled(false);
  ui.action_control_script_stopScript->setDisabled(true);
  ui.menu_control_turnTable->setDisabled(false);
  ui.menu_control_thermostat->setDisabled(false);
  ui.menu_control_dataReader->setDisabled(false);
  ui.action_set_turnTableAndThermostat->setDisabled(false);
  ui.action_set_dataReader->setDisabled(false);
  ui.action_set_script->setDisabled(false);
  ui.action_set_parameter->setDisabled(false);
  showMessage(QStringLiteral("脚本已停止运行"));
}
void MemsAutoTestSoftware::slot_mEMSDataCurrentDataPackageype(MEMSDataCodec::DataPackagetype dataPackagetype)
{
  switch (dataPackagetype)
    {
    case MEMSDataCodec::Single:
      showMessage(QStringLiteral("当前MEMS数据收集协议采用单组数据模式"));
      break;
    case MEMSDataCodec::Multi:
      showMessage(QStringLiteral("当前MEMS数据收集协议采用多组数据模式"));
      break;
    default:
      break;
    }
}
//接收数据采集器反馈
void MemsAutoTestSoftware::slot_mEMSDataCollectionState(bool collectionState)
{
  if(collectionState)
    {
      ui.action_control_dataReader_startCollection->setDisabled(true);
      ui.action_control_dataReader_stopCollection->setDisabled(false);
      ui.action_set_dataReader->setDisabled(true);
      ui.label_mEMSData_stateFlag_collection->setStyleSheet("background:green");
      showMessage(QStringLiteral("MEMS数据采集已开始"));
    }
  else
    {
      ui.action_control_dataReader_startCollection->setDisabled(false);
      ui.action_control_dataReader_stopCollection->setDisabled(true);
      ui.label_mEMSData_stateFlag_collection->setStyleSheet("background:red");
      if (!scriptRunner->getScriptState())
        {
          ui.action_set_dataReader->setDisabled(false);
        }
      showMessage(QStringLiteral("MEMS数据采集已停止"));
    }
}

void MemsAutoTestSoftware::slot_mEMSDataCollectionProtocol(MEMSDataHandler::DataCollectionProtocol dataCollectionProtocol)
{
  switch (dataCollectionProtocol)
    {
    case MEMSDataHandler::Protocol_SingleTemperature:
      showMessage(QStringLiteral("当前MEMS数据收集协议采用单温度模式"));
      break;
    case MEMSDataHandler::Protocol_DoubleTemperature:
      showMessage(QStringLiteral("当前MEMS数据收集协议采用双温度模式"));
      break;
    default:
      break;
    }
}
void MemsAutoTestSoftware::slot_saveDataFileFailed(QString filePath)
{
  showMessage(QStringLiteral("保存MEMS数据到文件%1时失败").arg(filePath));
}
void MemsAutoTestSoftware::slot_mEMSDataToSave(int dataGroupIndex, QString data)
{
  showMessage(QStringLiteral("已保存第%1组MEMS数据：%2").arg(dataGroupIndex+1).arg(data));
}

void MemsAutoTestSoftware::slot_loadScriptFileFailed(QString filePath)
{
  showMessage(QStringLiteral("加载脚本文件%1失败").arg(filePath));
}
void MemsAutoTestSoftware::slot_saveScriptFileFailed(QString filePath)
{
  showMessage(QStringLiteral("保存脚本文件%1失败").arg(filePath));
}

//初始化信号槽，暂时由对象管理器调用
void MemsAutoTestSoftware::initSignalsAndSlots()
{
  //各个菜单按钮的信号槽
  connect(ui.action_file_saveScript, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_saveScript);
  connect(ui.action_file_loadScript, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_loadScript);
  connect(ui.action_file_about, &QAction::triggered, this, &MemsAutoTestSoftware::slot_file_about);
  connect(ui.action_file_quit, &QAction::triggered, [this]() {close(); });
  connect(ui.action_control_turnTable_connect, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_turnTable_connect);
  connect(ui.action_control_turnTable_disconnect, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_turnTable_disconnect);
  connect(ui.action_control_turnTable_powerOn, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_PowerOn, 0, 0, 0); });
  connect(ui.action_control_turnTable_powerOff, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_PowerOff, 0, 0, 0); });
  connect(ui.action_control_turnTable_closeCircuit, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_CloseCircuit, 0, 0, 0); });
  connect(ui.action_control_turnTable_openCircuit, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_OpenCircuit, 0, 0, 0); });
  connect(ui.action_control_turnTable_run, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_turnTable_run);
  connect(ui.action_control_turnTable_stop, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_Stop, 0, 0, 0); });
  connect(ui.action_control_turnTable_emergencyStop, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_EmergencyStop, 0, 0, 0); });
  connect(ui.action_control_thermostat_connect, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::Thermostat_Connect, 0, 0, 0); });
  connect(ui.action_control_thermostat_disconnect, &QAction::triggered, [this]() {turnTableCodec->sendInstruction(TurnTableCodec::Thermostat_Disconnect, 0, 0, 0); });
  connect(ui.action_control_thermostat_run, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_thermostat_run);
  connect(ui.action_control_dataReader_connect, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_dataReader_connect);
  connect(ui.action_control_dataReader_disconnect, &QAction::triggered, this, &MemsAutoTestSoftware::slot_control_dataReader_disconnect);
  connect(ui.action_control_dataReader_startCollection, &QAction::triggered, [this]() {mEMSDataHandler->slot_startDataCollection(); });
  connect(ui.action_control_dataReader_stopCollection, &QAction::triggered, [this]() {mEMSDataHandler->slot_stopDataCollection(); });
  connect(ui.action_control_script_startScript, &QAction::triggered, [this]() {scriptRunner->slot_startScript(); });
  connect(ui.action_control_script_stopScript, &QAction::triggered, [this]() {scriptRunner->slot_stopScript(); });
  connect(ui.action_set_turnTableAndThermostat, &QAction::triggered, [this]() {setTurnTableAndThermostatDialog->exec(); });
  connect(ui.action_set_dataReader, &QAction::triggered, [this]() {setMEMSDataDialog->exec(); });
  connect(ui.action_set_script, &QAction::triggered, [this]() {setScriptDialog->exec(); });
  connect(ui.action_set_parameter, &QAction::triggered, [this]() {parameterConsole->exec(); });
}

void MemsAutoTestSoftware::showMessage(QString msg)
{
  QListWidget* listWidget = ui.listWidget_message;
  listWidget->addItem(QDateTime::currentDateTime().toString("yyyy" + QStringLiteral("年") + "MM" + QStringLiteral("月") + "dd" + QStringLiteral("日") + "  hh:mm:ss  ") + msg);
  listWidget->setCurrentRow(listWidget->count() - 1);
  if (listWidget->count() > 100)
    {//if the count of listWidget's item over 100
      listWidget->takeItem(0);//remove the first one
    }
}

void MemsAutoTestSoftware::initAllUI()
{
  height();

  //setFixedSize(this->width(),this->height());
  Qt::WindowFlags windowFlag = Qt::Dialog;//最大化最小化和关闭按钮
  windowFlag |= Qt::WindowMinimizeButtonHint;
  windowFlag |= Qt::WindowMaximizeButtonHint;
  windowFlag |= Qt::WindowCloseButtonHint;
  setWindowFlags(windowFlag);
  //ui.statusBar->setSizeGripEnabled(false);//禁用状态栏右下角调整大小的拖拽点
  ui.label_turnTable_stateFlag_connection->setStyleSheet("background:red");
  ui.label_thermostat_stateFlag_connection->setStyleSheet("background:lightgray");

  //转台运行状态指示灯
  ui.label_turnTable_stateFlag_powerSupply->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_circuit->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_running->setStyleSheet("background:lightgray");
  ui.label_turnTable_stateFlag_amplifier->setStyleSheet("background:lightgray");

  //对应每个与转台连接相关的按钮
  ui.action_control_turnTable_connect->setDisabled(false);
  ui.action_control_turnTable_disconnect->setDisabled(true);
  ui.action_control_turnTable_powerOn->setDisabled(true);
  ui.action_control_turnTable_powerOff->setDisabled(true);
  ui.action_control_turnTable_closeCircuit->setDisabled(true);
  ui.action_control_turnTable_openCircuit->setDisabled(true);
  ui.action_control_turnTable_run->setDisabled(true);
  ui.action_control_turnTable_stop->setDisabled(true);
  ui.action_control_turnTable_emergencyStop->setDisabled(true);
  ui.action_control_thermostat_connect->setDisabled(true);
  ui.action_control_thermostat_disconnect->setDisabled(true);
  ui.action_control_thermostat_run->setDisabled(true);

  //数值显示
  ui.label_turnTable_currentPosition->setText("---");
  ui.label_turnTable_currentRate->setText("---");
  ui.lineEdit_turnTable_runMode->setText("---");
  ui.lineEdit_turnTable_targetPosition->setText("---");
  ui.lineEdit_turnTable_targetRate->setText("---");
  ui.lineEdit_turnTable_targetAcceleration->setText("---");
  ui.lineEdit_currentTemperature->setText("---");
  ui.lineEdit_targetTemperature->setText("---");
  ui.lineEdit_temperatureRate->setText("---");

  ui.action_control_dataReader_connect->setDisabled(false);
  ui.action_control_dataReader_disconnect->setDisabled(true);
  ui.label_mEMSData_stateFlag_connection->setStyleSheet("background:red");

  ui.action_control_dataReader_startCollection->setDisabled(false);
  ui.action_control_dataReader_stopCollection->setDisabled(true);
  ui.label_mEMSData_stateFlag_collection->setStyleSheet("background:red");
}

void MemsAutoTestSoftware::closeEvent(QCloseEvent* event)
{
  if (QMessageBox::question(this, QStringLiteral("退出"), QStringLiteral("确定要退出吗？")) == QMessageBox::Yes)
    {
      event->accept();
    }
  else
    {
      event->ignore();
    }
}
TurnTableRunningStateManager* MemsAutoTestSoftware::getTurnTableRunningStateManager()
{
  return turnTableRunningStateManager;
}
void MemsAutoTestSoftware::setTurnTableRunningStateManager(TurnTableRunningStateManager* turnTableRunningStateManager)
{
  this->turnTableRunningStateManager = turnTableRunningStateManager;
}

TurnTableCodec* MemsAutoTestSoftware::getTurnTableCodec()
{
  return turnTableCodec;
}
void MemsAutoTestSoftware::setTurnTableCodec(TurnTableCodec* turnTableCodec)
{
  if (Q_NULLPTR!=this->turnTableCodec)
    {
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableConnectionState, this, &MemsAutoTestSoftware::slot_updateTurnTableConnectionState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableCurrentState, this, &MemsAutoTestSoftware::slot_updateTurnTableCurrentState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_currentThermostatTemperature, this, &MemsAutoTestSoftware::slot_updateCurrentThermostatTemperature);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTablePowerState, this, &MemsAutoTestSoftware::slot_updateTurnTablePowerSupplyState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableCircuitState, this, &MemsAutoTestSoftware::slot_updateTurnTableCricuitState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableAmplifierState, this, &MemsAutoTestSoftware::slot_updateTurnTableAmplifierState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableRunningState, this, &MemsAutoTestSoftware::slot_updateTurnTableRunningState);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_instructionEcho, this, &MemsAutoTestSoftware::slot_instructionEcho);
      disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableError, this, &MemsAutoTestSoftware::slot_turnTableErrorEcho);
    }
  this->turnTableCodec = turnTableCodec;
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableConnectionState, this, &MemsAutoTestSoftware::slot_updateTurnTableConnectionState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableCurrentState, this, &MemsAutoTestSoftware::slot_updateTurnTableCurrentState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_currentThermostatTemperature, this, &MemsAutoTestSoftware::slot_updateCurrentThermostatTemperature);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTablePowerState, this, &MemsAutoTestSoftware::slot_updateTurnTablePowerSupplyState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableCircuitState, this, &MemsAutoTestSoftware::slot_updateTurnTableCricuitState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableAmplifierState, this, &MemsAutoTestSoftware::slot_updateTurnTableAmplifierState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableRunningState, this, &MemsAutoTestSoftware::slot_updateTurnTableRunningState);
  connect(this->turnTableCodec, &TurnTableCodec::signal_instructionEcho, this, &MemsAutoTestSoftware::slot_instructionEcho);
  connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableError, this, &MemsAutoTestSoftware::slot_turnTableErrorEcho);
}

MEMSDataCodec* MemsAutoTestSoftware::getMEMSDataCodec()
{
  return mEMSDataCodec;
}
void MemsAutoTestSoftware::setMEMSDataCodec(MEMSDataCodec* mEMSDataCodec)
{
  if (Q_NULLPTR!=this->mEMSDataCodec)
    {
      disconnect(this->mEMSDataCodec, &MEMSDataCodec::signal_currentDataPackageype, this, &MemsAutoTestSoftware::slot_mEMSDataCurrentDataPackageype);
    }

  this->mEMSDataCodec = mEMSDataCodec;
  connect(this->mEMSDataCodec, &MEMSDataCodec::signal_currentDataPackageype, this, &MemsAutoTestSoftware::slot_mEMSDataCurrentDataPackageype);
}

MEMSDataHandler* MemsAutoTestSoftware::getMEMSDataHandler()
{
  return mEMSDataHandler;
}
void MemsAutoTestSoftware::setMEMSDataHandler(MEMSDataHandler* mEMSDataHandler)
{
  if (Q_NULLPTR!= this->mEMSDataHandler)
    {
      disconnect(this->mEMSDataHandler, &MEMSDataHandler::signal_currentDataCollectionState, this, &MemsAutoTestSoftware::slot_mEMSDataCollectionState);
      disconnect(this->mEMSDataHandler, &MEMSDataHandler::signal_currentDataCollectionProtocol, this, &MemsAutoTestSoftware::slot_mEMSDataCollectionProtocol);
    }
  this->mEMSDataHandler = mEMSDataHandler;
  connect(this->mEMSDataHandler, &MEMSDataHandler::signal_currentDataCollectionState, this, &MemsAutoTestSoftware::slot_mEMSDataCollectionState);
  connect(this->mEMSDataHandler, &MEMSDataHandler::signal_currentDataCollectionProtocol, this, &MemsAutoTestSoftware::slot_mEMSDataCollectionProtocol);
}
MEMSDataSaver* MemsAutoTestSoftware::getMEMSDataSaver()
{
  return mEMSDataSaver;
}
void MemsAutoTestSoftware::setMEMSDataSaver(MEMSDataSaver* mEMSDataSaver)
{
  if (Q_NULLPTR!=this->mEMSDataSaver)
    {
      disconnect(this->mEMSDataSaver, &MEMSDataSaver::signal_mEMSDataToSave, this, &MemsAutoTestSoftware::slot_mEMSDataToSave);
    }
  this->mEMSDataSaver = mEMSDataSaver;
  connect(this->mEMSDataSaver,&MEMSDataSaver::signal_mEMSDataToSave,this,&MemsAutoTestSoftware::slot_mEMSDataToSave);
}
ScriptRunner* MemsAutoTestSoftware::getScriptRunner()
{
  return scriptRunner;
}
void MemsAutoTestSoftware::setScriptRunner(ScriptRunner* scriptRunner)
{
  if (Q_NULLPTR!= this->scriptRunner)
    {
      disconnect(this->scriptRunner, &ScriptRunner::signal_scriptStarted, this, &MemsAutoTestSoftware::slot_scriptIsStarted);
      disconnect(this->scriptRunner, &ScriptRunner::signal_scriptStoped, this, &MemsAutoTestSoftware::slot_scriptIsStoped);
      disconnect(this->scriptRunner, &ScriptRunner::signal_emptyScript, this, 0);
    }
  this->scriptRunner = scriptRunner;
  connect(this->scriptRunner, &ScriptRunner::signal_scriptStarted, this, &MemsAutoTestSoftware::slot_scriptIsStarted);
  connect(this->scriptRunner, &ScriptRunner::signal_scriptStoped, this, &MemsAutoTestSoftware::slot_scriptIsStoped);
  connect(this->scriptRunner, &ScriptRunner::signal_emptyScript, [this]() {showMessage(QStringLiteral("当前脚本为空，无法运行")); });
  ui.treeWidget_showScript->setScriptRunner(this->scriptRunner);
}
ScriptSaveLoader* MemsAutoTestSoftware::getScriptSaveLoader()
{
  return scriptSaveLoader;
}
void MemsAutoTestSoftware::setScriptSaveLoader(ScriptSaveLoader* scriptSaveLoader)
{
  if (Q_NULLPTR != this->scriptSaveLoader)
    {
      disconnect(this->scriptSaveLoader, &ScriptSaveLoader::signal_saveScriptFileFailed, this, &MemsAutoTestSoftware::slot_saveScriptFileFailed);
      disconnect(this->scriptSaveLoader, &ScriptSaveLoader::signal_loadScriptFileFailed, this, &MemsAutoTestSoftware::slot_loadScriptFileFailed);
    }
  this->scriptSaveLoader = scriptSaveLoader;
  connect(this->scriptSaveLoader, &ScriptSaveLoader::signal_saveScriptFileFailed, this, &MemsAutoTestSoftware::slot_saveScriptFileFailed);
  connect(this->scriptSaveLoader, &ScriptSaveLoader::signal_loadScriptFileFailed, this, &MemsAutoTestSoftware::slot_loadScriptFileFailed);
}
LogSaver* MemsAutoTestSoftware::getLogSaver()
{
  return logSaver;
}
void MemsAutoTestSoftware::setLogSaver(LogSaver* logSaver)
{
  this->logSaver = logSaver;
}
SerialPortManager* MemsAutoTestSoftware::getSerialPortManager_turnTable()
{
  return serialPortManager_turnTable;
}
void MemsAutoTestSoftware::setSerialPortManager_turnTable(SerialPortManager* serialPortManager_turnTable)
{
  this->serialPortManager_turnTable = serialPortManager_turnTable;
}
SerialPortManager* MemsAutoTestSoftware::getSerialPortManager_mEMSData()
{
  return serialPortManager_mEMSData;
}
void MemsAutoTestSoftware::setSerialPortManager_mEMSData(SerialPortManager* serialPortManager_mEMSData)
{
  this->serialPortManager_mEMSData = serialPortManager_mEMSData;
}

SetTurnTableAndThermostatDialog* MemsAutoTestSoftware::getSetTurnTableAndThermostatDialog()
{
  return setTurnTableAndThermostatDialog;
}
void MemsAutoTestSoftware::setSetTurnTableAndThermostatDialog(SetTurnTableAndThermostatDialog* setTurnTableAndThermostatDialog)
{
  this->setTurnTableAndThermostatDialog = setTurnTableAndThermostatDialog;
}
SetMEMSDataDialog* MemsAutoTestSoftware::getSetMEMSDataDialog()
{
  return setMEMSDataDialog;
}
void MemsAutoTestSoftware::setSetMEMSDataDialog(SetMEMSDataDialog* setMEMSDataDialog)
{
  this->setMEMSDataDialog = setMEMSDataDialog;
}
SetScriptDialog* MemsAutoTestSoftware::getSetScriptDialog()
{
  return setScriptDialog;
}
void MemsAutoTestSoftware::setSetScriptDialog(SetScriptDialog* setScriptDialog)
{
  this->setScriptDialog = setScriptDialog;
}
ParameterConsole* MemsAutoTestSoftware::getParameterConsole()
{
  return parameterConsole;
}
void MemsAutoTestSoftware::setParameterConsole(ParameterConsole* parameterConsole)
{
  this->parameterConsole = parameterConsole;
}
int MemsAutoTestSoftware::getAccuracy()
{
  return accuracy;
}

void MemsAutoTestSoftware::setAccuracy(int accuracy)
{
  this->accuracy = accuracy;
}
